<?php
namespace App\Service\Admin;

class UserService extends BaseService
{
    //用户

    /**
     * 列表
     * @param array $pages【start，limit】
     */
    public function getIndex($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbUser)->where('del', 0);
        if (isset($pas['off']) && $pas['off'] != '') {
            $db = $db->where('off', $pas['off']);
        }
        if (isset($pas['is_realname']) && $pas['is_realname'] != '') {
            $db = $db->where('is_realname', $pas['is_realname']);
        }
        if (isset($pas['realname']) && $pas['realname']) {
            $db = $db->where('realname', 'like', $pas['realname'].'%');
        }
        if (isset($pas['phone']) && $pas['phone']) {
            $db = $db->where('phone', 'like', $pas['phone'].'%');
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $sexs = $this->sexs;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $sex_name = array_key_exists($v['sex'], $sexs) ? $sexs[$v['sex']] : '-';
            $head = file_exists($v['head']) ? env('DOMAIN').'/'.$v['head'] : '';
            $id_front = file_exists($v['id_front']) ? env('DOMAIN').'/'.$v['id_front'] : '';
            $id_back = file_exists($v['id_back']) ? env('DOMAIN').'/'.$v['id_back'] : '';
            if ($v['is_realname'] == 2) {
                $real_name = '<b style="color: green">通过认证</b>';
            } else if ($v['is_realname'] == 1) {
                $real_name = '<b style="color: red">未通过认证</b>';
            } else {
                $real_name = '<b style="color: orangered">未认证</b>';
            }
            $arys[] = [
                'id' => $v['id'],
                'phone' => $v['phone'],
                'uname' => $v['uname'],
                'realname' => $v['realname'],
                'sex' => $v['sex'],
                'sex_name' => $sex_name,
                'head' => $head,
                'address_now' => $v['address_now'],
                'id_card' => $v['id_card'],
                'id_front' => $id_front,
                'id_back' => $id_back,
                'wx_account' => $v['wx_account'],
                'zfb_account' => $v['zfb_account'],
                'is_realname' => $v['is_realname'],
                'real_name' => $real_name,
                'off' => $v['off'],
                'disable' => $v['disable'],
                'disable_name' => $v['disable'] ? '禁用' : '启用',
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 详情
     */
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbUser)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $sexs = $this->sexs;
        $sex_name = array_key_exists($res['sex'], $sexs) ? $sexs[$res['sex']] : '-';
        $head = file_exists($res['head']) ? $res['head'] : '';
        $id_front = file_exists($res['id_front']) ? $res['id_front'] : '';
        $id_back = file_exists($res['id_back']) ? $res['id_back'] : '';
        if ($res['is_realname'] == 2) {
            $real_name = '<b style="color: green">通过认证</b>';
        } else if ($res['is_realname'] == 1) {
            $real_name = '<b style="color: red">未通过认证</b>';
        } else {
            $real_name = '<b style="color: orangered">未认证</b>';
        }
        $arys = [
            'id' => $res['id'],
            'phone' => $res['phone'],
            'uname' => $res['uname'],
            'realname' => $res['realname'],
            'sex' => $res['sex'],
            'sex_name' => $sex_name,
            'head' => $head,
            'address_now' => $res['address_now'],
            'id_card' => $res['id_card'],
            'id_front' => $id_front,
            'id_back' => $id_back,
            'wx_account' => $res['wx_account'],
            'zfb_account' => $res['zfb_account'],
            'is_realname' => $res['is_realname'],
            'real_name' => $real_name,
            'off' => $res['off'],
            'uptime' => $res['uptime'],
        ];
        return $this->toback(0, '', $arys);
    }

    //添加
    public function add($pas)
    {
        $res = (array) $this->dbConn($this->dbUser)
            ->where('del', 0)
            ->where('phone', $pas['phone'])
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '手机号已存在');
        }
        $pas['uptime'] = $pas['addtime'] = toDate();
        $up = $this->dbConn($this->dbUser)->insertGetId($pas);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    //修改
    public function modify($id, $pas)
    {
        $res = (array) $this->dbConn($this->dbUser)
            ->where('del', 0)
            ->where('id', '<>', $id)
            ->where('phone', $pas['phone'])
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '手机号已被使用');
        }
        $res = (array) $this->dbConn($this->dbUser)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '用户不存在');
        }
        if ($res['off']) {
            return $this->toback(-2, '已注销');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbUser)->where('id', $id)->update($pas);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    //实名审核
    public function authReal($id, $auth)
    {
        $res = (array) $this->dbConn($this->dbUser)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有用户');
        }
        if ($res['off'] == 1) {
            return $this->toback(-2, '用户已注销');
        }
        if ($res['is_realname'] == 2) {
            return $this->toback(-2, '用户已实名');
        }
        $up = $this->dbConn($this->dbUser)->where('id', $id)
            ->update(['is_realname' => $auth, 'realtime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    //删除
    public function del($id)
    {
        $res = (array) $this->dbConn($this->dbUser)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有用户');
        }
        $this->dbConn($this->dbUser)->where('id', $id)->update(['del' => 1]);

        return $this->toback();
    }

    //禁用启用
    public function able($id, $able)
    {
        $res = (array) $this->dbConn($this->dbUser)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $this->dbConn($this->dbUser)
            ->where('id', $id)
            ->update(['disable' => $able]);
        return $this->toback();
    }
}